/**
 * Created by Administrator on 2017/1/16.
 */
$(function jsValidateForm() {

    var form1 = $('#product_single');
    var $edit_form = $('#editor_form');

    var form2 = $('#color-img');

    form2.validate({
        submitHandler:function(form2){
            if (App) {
                App.blockUI($('.portlet'));
                if ($('.modal')) App.blockUI($('.modal'));

            } else {
                console.log('Please use after App.js initialized')
            }

            form2.submit();
        },
        errorElement: 'span', //default input error message container
        errorClass: 'help-inline', // default input error message class
        focusInvalid: false, // do not focus the last invalid input
        ignore: ".doon-ignore",
        rules: {
            product_id: {
                required: true
            },

            color_id: {
                required: true
            },

            color_img :{
                required:
                    function () {
                        return $('#color_img').attr('src') == '';
                    }
            },
        },
        messages:{
            product_id: {
                required: '请选择商品'
            },

            color_id: {
                required: '请选择颜色'
            },

            color_img :{
                required: '请选择图片'
            },
        },

        highlight: function (element) { // hightlight error inputs
            $(element)
                .closest('.help-inline').removeClass('ok'); // display OK icon
            $(element)
                .closest('.control-group').removeClass('success').addClass('error'); // set error class to the control group
        },

        unhighlight: function (element) { // revert the change dony by hightlight
            $(element)
                .closest('.control-group').removeClass('error'); // set error class to the control group
        },

        success: function (label) {
            label
                .addClass('valid').addClass('help-inline ok') // mark the current input as valid and display OK icon
                .closest('.control-group').removeClass('error').addClass('success'); // set success class to the control group

        }

    });

    $(".control-group.success input").css("color", "#333");
    $(".control-group.success .control-label").css("color", "#333");

    form1.validate({
        submitHandler: function (form1) {
            if (App) {
                App.blockUI($('.portlet'));
                if ($('.modal')) App.blockUI($('.modal'));

            } else {
                console.log('Please use after App.js initialized')
            }

            form1.submit();
        },
        errorElement: 'span', //default input error message container
        errorClass: 'help-inline', // default input error message class
        focusInvalid: false, // do not focus the last invalid input
        ignore: ".doon-ignore",
        rules: {
            brand_id: {
                required: true
            },
            period_id: {
                required: true
            },
            material_id: {
                required: true
            },
            'color_ids[]': {
                required: true
            },
            code: {
                required: true
            },
            diameter: {
                required: true
            },
            base_curve: {
                required: true
            },
            water_percent: {
                required: true
            },
            function_id: {
                required: true
            },
            first_title: {
                required: true
            },
            second_title: {
                required: true
            },
            promotion_sale_price: {
                required: true,
                number: true
            },
            sale_price: {
                required: true,
                number: true
            },
            stock: {
                required: true,
                digits: true
            },
            stock_detail: {
                required: true
            },
            sale_at: {
                required: true
            },
            upper_limit: {
                required: true
            },
            lower_limit: {
                required: true
            },
            super_ratio: {
                required: true
            },
            ancestor_ratio: {
                required: true
            },
            // sort : {
            //     required : function(){
            //         return $('.sort-by').val();
            //     }
            // },
            thumb_img: {
                required: true
            }
        },
        messages: {
            brand_id: {
                required: '必选'
            },
            period_id: {
                required: '必选'
            },
            material_id: {
                required: '必选'
            },
            'color_ids[]': {
                required: '必选'
            },
            code: {
                required: '必填字段'
            },
            diameter: {
                required: '必填字段'
            },
            water_percent: {
                required: '必填字段'
            },
            function_id: {
                required: '必选'
            },
            first_title: {
                required: '必填字段'
            },
            second_title: {
                required: '必填字段'
            },
            promotion_sale_price: {
                required: '必填字段',
                number: '必须为金额'
            },
            sale_price: {
                required: '必填字段',
                number: '必须为金额'
            },
            stock: {
                required: '必填字段',
                digits: '必须为数量'
            },
            stock_detail: {
                required: '必填字段'
            },
            sale_at: {
                required: '必选'
            },
            upper_limit: {
                required: '必填字段'
            },
            lower_limit: {
                required: '必填字段'
            },
            super_ratio: {
                required: '必填字段'
            },
            ancestor_ratio: {
                required: '必填字段'
            },
            // sort: {
            //     required: '必须为整数'
            // },
            thumb_img: {
                required: '请选择图片'
            }
        },


        highlight: function (element) { // hightlight error inputs
            $(element)
                .closest('.help-inline').removeClass('ok'); // display OK icon
            $(element)
                .closest('.control-group').removeClass('success').addClass('error'); // set error class to the control group
        },

        unhighlight: function (element) { // revert the change dony by hightlight
            $(element)
                .closest('.control-group').removeClass('error'); // set error class to the control group
        },

        success: function (label) {
            label
                .addClass('valid').addClass('help-inline ok') // mark the current input as valid and display OK icon
                .closest('.control-group').removeClass('error').addClass('success'); // set success class to the control group

        }

    });

    $edit_form.validate({
        submitHandler: function (form1) {
            if (App) {
                App.blockUI($('.portlet'));
                if ($('.modal')) App.blockUI($('.modal'));

            } else {
                console.log('Please use after App.js initialized')
            }

            form1.submit();
        },
        errorElement: 'span', //default input error message container
        errorClass: 'help-inline', // default input error message class
        focusInvalid: false, // do not focus the last invalid input
        ignore: ".doon-ignore",
        rules: {
            brand_id: {
                required: true
            },
            period_id: {
                required: true
            },
            material_id: {
                required: true
            },
            'color_ids[]': {
                required: true
            },
            code: {
                required: true
            },
            diameter: {
                required: true
            },
            base_curve: {
                required: true
            },
            water_percent: {
                required: true
            },
            function_id: {
                required: true
            },
            first_title: {
                required: true
            },
            second_title: {
                required: true
            },
            promotion_sale_price: {
                required: true,
                number: true
            },
            sale_price: {
                required: true,
                number: true
            },
            stock: {
                required: true,
                digits: true
            },
            stock_detail: {
                required: true
            },
            sale_at: {
                required: true
            },
            upper_limit: {
                required: true
            },
            lower_limit: {
                required: true
            },
            super_ratio: {
                required: true
            },
            ancestor_ratio: {
                required: true
            },
            // sort : {
            //     number : function(){
            //         return Math.pow($('.sort-by').val(),32);
            //     }
            // },
            // thumb_img: {
            //     required: true
            // }
        },
        messages: {
            brand_id: {
                required: '必选'
            },
            period_id: {
                required: '必选'
            },
            material_id: {
                required: '必选'
            },
            'color_ids[]': {
                required: '必选'
            },
            code: {
                required: '必填字段'
            },
            diameter: {
                required: '必填字段'
            },
            water_percent: {
                required: '必填字段'
            },
            function_id: {
                required: '必选'
            },
            first_title: {
                required: '必填字段'
            },
            second_title: {
                required: '必填字段'
            },
            promotion_sale_price: {
                required: '必填字段',
                number: '必须为金额'
            },
            sale_price: {
                required: '必填字段',
                number: '必须为金额'
            },
            stock: {
                required: '必填字段',
                digits: '必须为数量'
            },
            stock_detail: {
                required: '必填字段'
            },
            sale_at: {
                required: '必选'
            },
            upper_limit: {
                required: '必填字段'
            },
            lower_limit: {
                required: '必填字段'
            },
            super_ratio: {
                required: '必填字段'
            },
            ancestor_ratio: {
                required: '必填字段'
            },
            // sort: {
            //     number: '必须为整数'
            // },
            // thumb_img: {
            //     required: '请选择图片'
            // }
        },


        highlight: function (element) { // hightlight error inputs
            $(element)
                .closest('.help-inline').removeClass('ok'); // display OK icon
            $(element)
                .closest('.control-group').removeClass('success').addClass('error'); // set error class to the control group
        },

        unhighlight: function (element) { // revert the change dony by hightlight
            $(element)
                .closest('.control-group').removeClass('error'); // set error class to the control group
        },

        success: function (label) {
            label
                .addClass('valid').addClass('help-inline ok') // mark the current input as valid and display OK icon
                .closest('.control-group').removeClass('error').addClass('success'); // set success class to the control group
        }

    });

});